if orders and not orders.GetLimit then


function orders:GetLimit(order_num)
	if order_num and order_num > 0 then
		local limits = self:GetLimits()
		
		return limits[order_num]
	end
end


function orders:GetLimits(sec_code, side, price)
	local instrument = lib:GetInstrument(sec_code)
	local list = {}
	local o
	
	for i = getNumberOf("orders") - 1, 0, -1 do
		o = getItem("orders", i)
		if (not instrument or o.sec_code == instrument.sec_code)
			and self:IsOrder(o)
			and bit.band(o.flags, 9) == 9	-- active limit
			and (side == nil or side == not bit.test(o.flags, 2))
			and (not price or math.abs(price - o.price) < instrument.min_price_step)
		then
			--table.insert(list, o)
			list[o.order_num] = o
		end
	end
	
	return list
end


function orders:GetShiftPrice(instrument, is_long)
	--message("GetShiftPrice call " .. tostring(instrument) .. " " .. tostring(is_long))
	if is_long ~= nil then
		instrument = lib:GetInstrument(instrument)
			
		if instrument then
			local sp, bid, ask = lib:GetSpread(instrument)
			
			if sp then
				if is_long then
					sp = lib:NormalizePrice(ask - instrument.min_price_step, instrument)
				else
					sp = lib:NormalizePrice(bid + instrument.min_price_step, instrument)
				end
				
				--message("GetShiftPrice " .. instrument.code .. " " .. tostring(sp))
				
				return sp
			else
				fail("GetShiftPrice no spread of " .. instrument.code)
			end
		end
	end
end


function orders:SetShiftPrice(order)
	--message("SetShiftPrice call " .. tostring(order))
	if type(order) == "table" then
		local instrument = lib:GetInstrument(order.sec_code or order.SECCODE)
		
		if instrument then
			local sp = order.price or tonumber(order.PRICE) or 0
			local is_long = order.flags and not bit.test(order.flags, 2) or order.OPERATION and order.OPERATION == "B"
			local price = self:GetShiftPrice(instrument, is_long)
			
			if price then
				if is_long and (sp <= 0 or sp > price)
					or not is_long and (sp < price)
				then
					order.price = price
					if order.PRICE then
						order.PRICE = lib:FormatPrice(price, instrument)
					end
					
					--self.log(message("SetShiftPrice shift price from " .. sp .. " to " .. order.PRICE))
						
					return price
				end
			end
		end
	end
end


function orders:GetLastTradePrice(sec_code, is_long)
	if type(sec_code) == "table" then
		sec_code = sec_code.sec_code or sec_code.code
	end
	
	if type(sec_code) == "string" then
		local tr
		
		for i = getNumberOf("trades") - 1, 0, -1 do
			tr = getItem("trades", i)
			if tr.sec_code == sec_code and (is_long == nil or (not is_long) == bit.test(tr.flags, 2)) then
				return tr.price, not bit.test(tr.flags, 2), tr
			end
		end
	end
end


end -- of file
